const Message = require('../model/message')

module.exports = {
  /**
   * 添加消息的中间件
   * @param request
   * @param response
   * @param next
   */
  sMessage: (request, response, next) => {
    let {message} = request.body
    Message.setMessage(message)
      .then(result => {
        request.add_mid = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 删除消息中间件
   * @param request
   * @param response
   * @param next
   */
  dMessage: (request, response, next) => {
    let {id} = request.body

    Message.deleteMessage(id)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定消息条数的中间件
   * @param request
   * @param response
   * @param next
   */
  gMessageCount: (request, response, next) => {
    let {username, handle} = request.body
    Message.getMessageCount(username, handle)
      .then(result => {
        request.messageCount = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定消息条数信息的中间件
   * @param request
   * @param response
   * @param next
   */
  gMessage: (request, response, next) => {
    let start = response.start
    let size = response.size
    let {username, handle} = request.body
    Message.getMessage(start, size, username, handle)
      .then(result => {
        request.recordCountMessage = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 修改消息中间件
   * @param request
   * @param response
   * @param next
   */
  uMessage: (request, response, next) => {
    let {message} = request.body

    Message.updateMessage(message)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }
}